SHELL:=/bin/bash
COMPOSE_DIR := ./docker-compose-file
COMPOSE_FILE := ${COMPOSE_DIR}/org1_peer2.yaml
ORG2_COMPOSE_DIR := ./org2
ORG2_COMPOSE_FILE := ${ORG2_COMPOSE_DIR}/docker-compose.yaml

download:
	@echo "Download Docker images"
	bash scripts/download_images.sh

clean:
	@-docker ps -a | awk '{ print $$1,$$2 }' | grep "hyperledger/fabric" | awk '{ print $$1 }' | xargs -r -I {} docker rm -f {}
	@-docker ps -a | awk '$$2 ~ /dev-peer/ { print $$1 }' | xargs -r -I {} docker rm -f {}
	@-docker images | awk '$$1 ~ /dev-peer/ { print $$3 }' | xargs -r -I {} docker rmi -f {}
	@echo "clean success"

gen_config_crypto:
	bash scripts/gen_config_crypto.sh

gen_config_channel:
	bash scripts/gen_config_channel.sh

clean_config:
	rm -rf crypto-config
	rm -rf channel-artifacts

gen_config:
	@make clean_config
	@make gen_config_crypto
	@make gen_config_channel

start:
	@echo "Start a fabric network with ${COMPOSE_FILE}..."
	@make clean
	@echo "Make sure the local hlf_net docker bridge exists"
	docker network ls|grep hlf_net > /dev/null || docker network create hlf_net
	@docker-compose -f ${COMPOSE_FILE} --env-file ${COMPOSE_DIR}/.env up -d

stop:
	@docker-compose -f ${COMPOSE_FILE} down
	@make clean

cli:
	docker exec -it fabric-cli bash

clean_docker:
	docker volume prune

add_new_org:
	@docker exec -it fabric-cli bash -c "cd /tmp; bash scripts/add_new_org.sh"

gen_new_org_config:
	@docker exec -it fabric-cli bash -c "cd /tmp; bash scripts/gen_new_org_config.sh"

start_org2:
	@docker-compose -f ${ORG2_COMPOSE_FILE} up -d

stop_org2:
	@docker-compose -f ${ORG2_COMPOSE_FILE} down

run_go:
	@docker exec -it fabric-cli bash -c "cd /go/src/fabric_client; go run *.go"

fetch_new:
	@docker exec -it fabric-cli bash -c "cd /tmp; bash scripts/fetch_new.sh"